package cloud

import (
	"encoding/json"
	"iot-base/auth/model"
	"iot-base/auth/svc"
	types2 "iot-base/auth/types"
)

// AddCloudTask 添加任务
func AddCloudTask(params []byte) ([]byte, error) {
	var task model.Tasks
	err := json.Unmarshal(params, &task)
	if err != nil {
		return nil, err
	}
	err = svc.Dao.TTasks.Add(&task)
	if err != nil {
		return nil, err
	}
	if err != nil {
		return nil, err
	}
	return nil, nil
}

// DelCloudTask 删除任务.
func DelCloudTask(params []byte) ([]byte, error) {
	var req types2.ReqDelTaskByCodeId
	err := json.Unmarshal(params, &req)
	if err != nil {
		return nil, err
	}
	err = svc.Dao.TTasks.DeleteByCodeId(req.CodeId)
	if err != nil {
		return nil, err
	}
	return nil, nil
}

// ModifyCloudTask 修改任务
func ModifyCloudTask(params []byte) ([]byte, error) {
	var req model.Tasks
	err := json.Unmarshal(params, &req)
	if err != nil {
		return nil, err
	}
	req.ID = 0
	err = svc.Dao.TTasks.UpdateByCodeId(req.CodeId, req)
	if err != nil {
		return nil, err
	}
	return nil, nil
}

func AddTaskLog(params []byte) ([]byte, error) {
	var req model.TaskLog
	err := json.Unmarshal(params, &req)
	if err != nil {
		return nil, err
	}
	_, err = svc.Dao.TTaskLog.Add(req)
	return nil, err
}

func ModifyTaskLog(params []byte) ([]byte, error) {
	var req model.TaskLog
	err := json.Unmarshal(params, &req)
	if err != nil {
		return nil, err
	}
	var create model.Datetime
	req.CreatedAt = create
	err = svc.Dao.TTaskLog.Update(req.ID, req)
	return nil, err
}

func AddTaskLogDetails(params []byte) ([]byte, error) {
	var req model.TaskLogDetails
	err := json.Unmarshal(params, &req)
	if err != nil {
		return nil, err
	}
	_, err = svc.Dao.TTaskLogDetail.Add(req)
	return nil, err
}

func ModifyTaskLogDetails(params []byte) ([]byte, error) {
	var req model.TaskLogDetails
	err := json.Unmarshal(params, &req)
	if err != nil {
		return nil, err
	}

	var create model.Datetime
	req.CreatedAt = create

	err = svc.Dao.TTaskLogDetail.Update(req.ID, req)
	return nil, err
}
